<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1;

use App\Controller\Admin\AdminController;
use App\Model\Job;
use App\Model\Order;
use App\Model\PositionSupervise;
use App\Model\User;
use App\Model\UserAccountRecord;
use Carbon\Carbon;
use Hyperf\DbConnection\Db;

class HomeController extends AdminController
{
    public function index()
    {
        $yesterday_time = Carbon::yesterday();
        $start_time=Carbon::today();
        $end_time=Carbon::tomorrow();

        $all_user_nums = User::query()->where('is_virtual',0)->count('id');
        $today_user_nums = User::query()->where('created_at','>=',$start_time)
            ->where('is_virtual',0)
            ->where('created_at','<',$end_time)->count('id');

        $yesterday_user_nums = User::query()->where('created_at','>=',$yesterday_time)
            ->where('is_virtual',0)
            ->where('created_at','<',$start_time)->count('id');

        $all_supervise_nums = PositionSupervise::query()->count('id');
        $today_supervise_nums = PositionSupervise::query()->where('created_at','>=',$start_time)
            ->where('created_at','<',$end_time)->count('id');

        $yesterday_supervise_nums = PositionSupervise::query()->where('created_at','>=',$yesterday_time)
            ->where('created_at','<',$start_time)->count('id');


        //近七日用户数据
        $seven_start_days = date("Y-m-d",strtotime("-7 days")).' 00:00:00';
        $seven_end_days = Carbon::yesterday()->toDateString();
        $seven_days =[];
        for($i=0;$i<7;$i++){
            $date = date('Y-m-d',strtotime("+$i day",strtotime($seven_start_days)));
            $seven_days[$i]=$date;
        }
        $user_static_query = User::query()->select(DB::raw('left(created_at,10) as date'),DB::raw('count(id) as userNum'))
            ->where('created_at', '>=', $seven_start_days . ' 00:00:00')
            ->where('created_at', '<', $seven_end_days . ' 23:59:59')
            ->where('is_virtual',0)
            ->groupBy('date')
            ->get()
            ->keyBy('date')
            ->toArray();
        $user_static=[];
        foreach ($seven_days as $k=>$day){
            $user_static[$k]['x'] = date('m-d',strtotime($day));
            if(array_key_exists($day,$user_static_query)){
                $user_static[$k]['y'] = $user_static_query[$day]['userNum'];
            }else{
                $user_static[$k]['y'] = 0;
            }
        }

        $supervise_static_query = PositionSupervise::query()->select(DB::raw('left(created_at,10) as date'),DB::raw('count(id) as superviseNum'))
            ->where('created_at', '>=', $seven_start_days . ' 00:00:00')
            ->where('created_at', '<', $seven_end_days . ' 23:59:59')
            ->groupBy('date')
            ->get()
            ->keyBy('date')
            ->toArray();
        $supervise_static=[];
        foreach ($seven_days as $k=>$day){
            $supervise_static[$k]['x'] = date('m-d',strtotime($day));
            if(array_key_exists($day,$supervise_static_query)){
                $supervise_static[$k]['y'] = $supervise_static_query[$day]['superviseNum'];
            }else{
                $supervise_static[$k]['y'] = 0;
            }
        }


        $return=[
            'all_supervise_nums' => $all_supervise_nums,
            'today_supervise_nums' => $today_supervise_nums,
            'yesterday_supervise_nums' => $yesterday_supervise_nums,//昨日订单数
            'all_user_nums' => $all_user_nums,//总用户数
            'today_user_nums' => $today_user_nums,//今日用户数
            'yesterday_user_nums' => $yesterday_user_nums,//昨日用户数
            'user_static' => $user_static,
            'supervise_static' => $supervise_static
        ];
        return $this->response->success($return);
    }
}